package com.goods.controller.business;


import com.goods.business.controller.*;
import com.goods.business.mapper.*;
import com.goods.business.pojo.*;
import com.goods.common.error.SystemException;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.JWTUtils;
import com.goods.common.vo.business.*;
import com.goods.common.vo.system.UserInfoVO;
import com.goods.system.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/business")
public class BusinessWbe {
    @Autowired
    StudentControllertt studentControllertt;
    @Autowired
    SupplierController supplierController;
    @Autowired
    ProductController productController;
    @Autowired
    ProductMapper productMapper;
    @Autowired
    InStockController inStockController;
    @Autowired
    InStockMapper inStockMapper;
    @Autowired
    ProductStockMapper productStockMapper;
    @Autowired
    ConsumerController consumerController;
    @Autowired
    ConsumerMapper consumerMapper;
    @Autowired
    OutStockController outStockController;
    @Autowired
    OutStockMapper outStockMapper;
    @Autowired
    SupplierMapper supplierMapper;
    @Autowired
    UserService userService;

    @GetMapping("productCategory/categoryTree")
    public ResponseBean productCategory(@RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize) {
        if (StringUtils.isEmpty(pageNum) && StringUtils.isEmpty(pageSize)) {
            List<ProductCategoryTreeNodeVO> parentCategoryTree = studentControllertt.getParentCategoryTree();
            HashMap<String, Object> map = new HashMap<>();
            map.put("rows", parentCategoryTree);
            return ResponseBean.success(map);
        }
        List<ProductCategoryTreeNodeVO> selepage = studentControllertt.selepage(pageNum, pageSize);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", selepage);
        map.put("rows", selepage);
        return ResponseBean.success(map);
    }

    //    http://www.localhost:8989/business/productCategory/getParentCategoryTree
    @GetMapping("productCategory/getParentCategoryTree")
    public ResponseBean getParentCategoryTree() {
        return ResponseBean.success(studentControllertt.getParentCategoryTree());
    }

    //    http://www.localhost:8989/business/productCategory/add
    @PostMapping("productCategory/add")
    public ResponseBean add(@RequestBody Map map) {
        studentControllertt.add(map);
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/productCategory/delete/33
    @DeleteMapping("productCategory/delete/{id}")
    public ResponseBean delById(@PathVariable Integer id) {
        Integer integer = studentControllertt.delById(id);
        if (integer == 0) {

            return ResponseBean.success();
        } else {
            Map<Object, Object> map = new HashMap<>();
            map.put("errorMsg", "存在子类,无法删除!");
            return ResponseBean.error(map);
        }
    }

    //http://www.localhost:8989/business/productCategory/edit/24
    @GetMapping("productCategory/edit/{id}")
    public ResponseBean selectbyId(@PathVariable Long id) {
        Student student = studentControllertt.selectById(id);
        return ResponseBean.success(student);
    }

    //    http://www.localhost:8989/business/productCategory/update/24
    @PutMapping("productCategory/update/{id}")
    public ResponseBean updateById(@PathVariable Long id, @RequestBody ProductCategoryVO map) {
        studentControllertt.updatebyId(id, map);
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/supplier/findSupplierList?pageNum=1&pageSize=10&name=
//    http://www.localhost:8989/business/supplier/findSupplierList?pageNum=1&pageSize=10
    // &name=changping&contact=zhangsan&address=beijing
    @GetMapping("supplier/findSupplierList")
    public ResponseBean findSupplierList(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                         @RequestParam String name) {

        List<Bizsupplier> supplierList = supplierController.findSupplierList(pageNum, pageSize, name);
        if (CollectionUtils.isEmpty(supplierList)) {
            Map<Object, Object> map = new HashMap<>();
            map.put("errorMsg", "未查询到信息!");
            return ResponseBean.error(map);
        }
        HashMap<String, Object> map = new HashMap<>();
//        map.put("total",selepage);
        map.put("rows", supplierList);
        return ResponseBean.success(map);
    }

    //    http://www.localhost:8989/business/supplier/add
    @PostMapping("supplier/add")
    public ResponseBean supplierAdd(@RequestBody SupplierVO supplierVO) {
        supplierController.supplierAdd(supplierVO);
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/supplier/edit/24
    @GetMapping("supplier/edit/{id}")
    public ResponseBean selectSupplierById(@PathVariable Integer id) {
        return ResponseBean.success(supplierController.selectSupplierById(id));
    }

    //    http://www.localhost:8989/business/supplier/update/24
    @PutMapping("supplier/update/{id}")
    public ResponseBean updateSupplierById(@RequestBody Bizsupplier supplierVO, @PathVariable Integer id) {
        supplierController.updateSupplierById(supplierVO, id);
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/supplier/delete/24
    @DeleteMapping("supplier/delete/{id}")
    public ResponseBean delSupplierById(@PathVariable Integer id) {
        supplierController.delSupplierById(id);
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/product/findProductList?
//    pageNum=1&pageSize=6&name=&categoryId=&supplier=&status=0
    @GetMapping("product/findProductList")
    public ResponseBean findProductList(@RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                        @RequestParam String name, @RequestParam Integer categoryId,
                                        @RequestParam Integer status) {
        List<ProductVO> productList = productController.findProductList(pageNum, pageSize, name, categoryId, status);
        HashMap<String, Object> map = new HashMap<>();
//        map.put("total",selepage);
        map.put("rows", productList);
        return ResponseBean.success(map);
    }

    //    http://www.localhost:8989/business/product/add
    @PostMapping("product/add")
    public ResponseBean productAdd(@RequestBody ProductVO productVO) {
        productController.productAdd(productVO);
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/product/edit/38
    @GetMapping("product/edit/{id}")
    public ResponseBean selProductById(@PathVariable Integer id) {
        ProductVO productVO = productMapper.selectByPrimaryKey(id);

        return ResponseBean.success(productVO);
    }

    //    http://www.localhost:8989/business/product/update/37
    @PutMapping("product/update/{id}")
    public ResponseBean updateProductById(@PathVariable Integer id, @RequestBody ProductVO productVO) {
        productController.updateProductById(id, productVO);
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/product/remove/38
    @PutMapping("product/remove/{id}")
    public ResponseBean updateProductById(@PathVariable Integer id) {
        ProductVO productVO = productMapper.selectByPrimaryKey(id);
        productVO.setStatus(1);
        productMapper.updateByPrimaryKey(productVO);
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/product/back/51
    @PutMapping("product/back/{id}")
    public ResponseBean updateProductByIdBack(@PathVariable Integer id) {
        ProductVO productVO = productMapper.selectByPrimaryKey(id);
        productVO.setStatus(0);
        productMapper.updateByPrimaryKey(productVO);
        return ResponseBean.success();
    }

    //http://www.localhost:8989/business/product/publish/54
    @PutMapping("product/publish/{id}")
    public ResponseBean updateProductByIdpublish(@PathVariable Integer id) {
        ProductVO productVO = productMapper.selectByPrimaryKey(id);
        productVO.setStatus(0);
        productMapper.updateByPrimaryKey(productVO);
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/product/delete/54
    @DeleteMapping("product/delete/{id}")
    public ResponseBean delProductById(@PathVariable Integer id) {
        productMapper.deleteByPrimaryKey(id);
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/inStock/findInStockList?pageNum=1&pageSize=10&status=0
    @GetMapping("inStock/findInStockList")
    public ResponseBean inStockSelectQuery(@RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize, @RequestParam(required = false) Integer status) {
        List<InStockVO> inStockVOS = inStockController.inStockSelectQuery(pageNum, pageSize, status);
        HashMap<String, Object> map = new HashMap<>();
//        map.put("total",selepage);
        map.put("rows", inStockVOS);
        return ResponseBean.success(map);
    }

    //http://www.localhost:8989/business/inStock/detail/112?pageNum=1
    @GetMapping("inStock/detail/{id}")
    public ResponseBean selinStockDetaiList(@RequestParam(required = false) Integer pageNum, @PathVariable Integer id) {
        InStockDetailVO inStockDetailVO = inStockController.selinStockDetaiList(pageNum, id);
        return ResponseBean.success(inStockDetailVO);
    }

    //    http://www.localhost:8989/business/supplier/findAll
    @GetMapping("supplier/findAll")
    public ResponseBean supplierfindAll() {
        List<Bizsupplier> bizsuppliers = supplierMapper.selectAll();
        return ResponseBean.success(bizsuppliers);
    }

    //http://www.localhost:8989/business/product/findProducts?pageNum=1&pageSize=6&status=0
    @GetMapping("product/findProducts")
    public ResponseBean productfindProducts() {
        List<ProductVO> productVOS = productMapper.selectAll();
        InStockItemVO inStockItemVO = new InStockItemVO();
        Map<Object, Object> map = new HashMap<>();
        map.put("rows",productVOS);
        return ResponseBean.success(map);
    }

    //http://www.localhost:8989/business/inStock/addIntoStock
    @PostMapping("inStock/addIntoStock")
    public ResponseBean inStockAddIntoStock(@RequestBody InStockVO inStockVO) throws SystemException {
        inStockController.inStockAddIntoStock(inStockVO);
        return ResponseBean.success();
    }


    //http://www.localhost:8989/business/inStock/remove/112
    @PutMapping("inStock/remove/{id}")
    public ResponseBean inStockByIdremove(@PathVariable Integer id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(1);
        inStockMapper.updateByPrimaryKey(inStock);
        return ResponseBean.success();
    }



    //    http://www.localhost:8989/business/inStock/back/111
    @PutMapping("inStock/back/{id}")
    public ResponseBean inStockByIdback(@PathVariable Integer id) {
        InStock inStock = inStockMapper.selectByPrimaryKey(id);
        inStock.setStatus(0);
        inStockMapper.updateByPrimaryKey(inStock);
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/inStock/delete/111
    @GetMapping("inStock/delete/{id}")
    public ResponseBean delinStockByIdback(@PathVariable Integer id) {
        inStockMapper.deleteByPrimaryKey(id);
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/product/findAllStocks?pageSize=9&pageNum=1
    @GetMapping("product/findAllStocks")
    public ResponseBean findAllStocks() {
//    List<ProductStock> productStocks = productStockMapper.selectAll();
//    HashMap<String, Object> map = new HashMap<>();
////        map.put("total",selepage);
//    map.put("rows",productStocks);
//    return ResponseBean.success(productStocks);
        List<ProductVO> productVOS = productMapper.selectAll();
        List<ProductStock> productStocks = productStockMapper.selectAll();
        List<Map<Object, Object>> objects = new ArrayList<>();
        for (ProductVO productVO : productVOS) {
            for (ProductStock productStock : productStocks) {
                if (productVO.getPNum().toString().equals(productStock.getpNum().toString())) {
                    HashMap<Object, Object> map1 = new HashMap<>();
                    map1.put("name", productVO.getName());
                    map1.put("stock", productStock.getStock());
                    objects.add(map1);
                }
            }
        }
        return ResponseBean.success(objects);
    }


    //http://www.localhost:8989/business/product/findProductStocks?pageSize=9&pageNum=1
    @GetMapping("product/findProductStocks")
    public ResponseBean findProductStocks() {
        List<ProductVO> productVOS = productMapper.selectAll();
        List<ProductStock> productStocks = productStockMapper.selectAll();
        List<ProductStockVO> productStockVOS = new ArrayList<>();
        for (ProductVO productVO : productVOS) {
            for (ProductStock productStock : productStocks) {
                if (productVO.getPNum().toString().equals(productStock.getpNum().toString())) {
                    ProductStockVO productStockVO1 = new ProductStockVO();
                    productStockVO1.setImageUrl(productVO.getImageUrl());
                    productStockVO1.setModel(productVO.getModel());
                    productStockVO1.setName(productVO.getName());
                    productStockVO1.setPNum(productVO.getPNum());
                    productStockVO1.setRemark(productVO.getRemark());
                    productStockVO1.setStock(productStock.getStock());
                    productStockVO1.setUnit(productVO.getUnit());
                    productStockVO1.setId(productVO.getId());
                    productStockVOS.add(productStockVO1);
                }
            }
        }

        HashMap<String, Object> map = new HashMap<>();
//        map.put("total",selepage);
        map.put("rows", productStockVOS);
        return ResponseBean.success(map);
    }

    //    http://www.localhost:8989/business/consumer/findConsumerList?pageNum=1&pageSize=10&name=
    //http://www.localhost:8989/business/consumer/findConsumerList?pageNum=1&pageSize=10&name=jutididian
// &address=beijing&contact=zhansan
    @GetMapping("consumer/findConsumerList")
    public ResponseBean findConsumerList(@RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize,
                                         @RequestParam(required = false) String name, @RequestParam(required = false) String address,
                                         @RequestParam(required = false) String contact) {
        List<ConsumerVO> consumerList = consumerController.findConsumerList(pageNum, pageSize, name, address, contact);
        HashMap<String, Object> map = new HashMap<>();
//        map.put("total",selepage);
        map.put("rows", consumerList);
        return ResponseBean.success(map);
    }

    //    http://www.localhost:8989/business/consumer/edit/13
    @GetMapping("consumer/edit/{id}")
    public ResponseBean selConsumerById(@PathVariable Integer id) {
        return ResponseBean.success(consumerMapper.selectByPrimaryKey(id));
    }

    //    http://www.localhost:8989/business/consumer/update/13
    @PutMapping("consumer/update/{id}")
    public ResponseBean putConsumerById(@PathVariable Long id, @RequestBody ConsumerVO consumerVO) {
        consumerVO.setModifiedTime(new Date());
        consumerVO.setId(id);
        return ResponseBean.success(consumerMapper.updateByPrimaryKey(consumerVO));
    }

    //    http://www.localhost:8989/business/consumer/delete/28
    @DeleteMapping("consumer/delete/{id}")
    public ResponseBean delConsumerById(@PathVariable Long id) {
        consumerMapper.deleteByPrimaryKey(id);
        return ResponseBean.success();
    }

    //http://www.localhost:8989/business/outStock/findOutStockList?pageNum=1&pageSize=10&status=0
    //http://www.localhost:8989/business/outStock/findOutStockList?pageNum=1&pageSize=10&status=2&type=0&outNum=72721441154944b7be35
    //http://www.localhost:8989/business/outStock/findOutStockList?pageNum=1&pageSize=10&status=0&type=&outNum=8fdfea67fb2d46e1ba26cdce363d
    @GetMapping("outStock/findOutStockList")
    public ResponseBean findOutStockList(@RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize,
                                         @RequestParam(required = false) Integer status, @RequestParam(required = false) String outNum,
                                         @RequestParam(required = false) Integer type) {
        List<OutStockVO> consumerList = outStockController.findOutStockList(pageNum, pageSize, status, outNum, type);
        HashMap<String, Object> map = new HashMap<>();
//        map.put("total",selepage);
        map.put("rows", consumerList);
        return ResponseBean.success(map);
    }



    //    http://www.localhost:8989/business/outStock/detail/16?pageNum=1
    @GetMapping("outStock/detail/{id}")
    public ResponseBean seloutStockdetailById(@RequestParam(required = false) Integer pageNum, @PathVariable Integer id) {
        OutStockDetailVO inStockDetailVO = outStockController.seloutStockdetailById(pageNum, id);
        return ResponseBean.success(inStockDetailVO);
    }


//    http://www.localhost:8989/business/consumer/findAll
    @GetMapping("consumer/findAll")
    public ResponseBean ConsumerFindAll(){
        List<ConsumerVO> consumerVOS = consumerMapper.selectAll();
        return ResponseBean.success(consumerVOS);
    }

//    http://www.localhost:8989/business/outStock/addOutStock
    @PostMapping("outStock/addOutStock")
    public ResponseBean OutStockAddOutStock(@RequestBody OutStockVO inStockVO) throws SystemException {
        outStockController.OutStockAddOutStock(inStockVO);
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/outStock/remove/15
    @PutMapping("outStock/remove/{id}")
    public ResponseBean removeStockdetailById(@PathVariable Integer id) {
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        outStock.setStatus(1);
        outStockMapper.updateByPrimaryKey(outStock);
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/outStock/back/15
    @PutMapping("outStock/back/{id}")
    public ResponseBean backStockdetailById(@PathVariable Integer id) {
        OutStock outStock = outStockMapper.selectByPrimaryKey(id);
        outStock.setStatus(0);
        outStockMapper.updateByPrimaryKey(outStock);
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/outStock/delete/15
    @GetMapping("outStock/delete/{id}")
    public ResponseBean deleteStockdetailById(@PathVariable Integer id) {
        outStockMapper.deleteByPrimaryKey(id);
        return ResponseBean.success();
    }

    //http://www.localhost:8989/business/health/isReport
    @GetMapping("health/isReport/{name}")
    public ResponseBean health(@PathVariable String name, HttpServletRequest httpServletRequest) throws SystemException {
        UserInfoVO info = userService.info();
        BizHealth health = outStockController.health(info.getUsername());
        if (!StringUtils.isEmpty(health)) {
            return ResponseBean.success(health);
        }
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/health/report
    @PostMapping("health/report/{name}")
    public ResponseBean healthReport(@PathVariable String name, @RequestBody BizHealth bizHealth) throws SystemException {
        UserInfoVO info = userService.info();
        Date date = outStockController.healthReport(info.getUsername(), bizHealth);
        if (!StringUtils.isEmpty(date)) {
            Map<Object, Object> map = new HashMap<>();
            map.put("nowTime", date);
            return ResponseBean.success(map);
        }
        return ResponseBean.success();
    }

    //    http://www.localhost:8989/business/health/history/admin?pageSize=4&pageNum=1
    @GetMapping("health/history/{admin}")
    public ResponseBean selhealthHistoryall(@PathVariable String admin, @RequestParam(required = false) Integer pageNum, @RequestParam(required = false) Integer pageSize) throws SystemException {
        UserInfoVO info = userService.info();
        List<BizHealth> bizHealths = outStockController.selhealthHistoryall(info.getUsername(), pageNum, pageSize);
        HashMap<Object, Object> map = new HashMap<>();
        map.put("rows", bizHealths);
        map.put("total", 2);
        return ResponseBean.success(map);
    }
}














